package com.jy.appmarket.screenlock.activity;

import com.jy.appmarket.R;
import com.jy.appmarket.screenlock.prefs.LockPrefs;
import com.jy.appmarket.screenlock.prefs.LockPrefs.PasswordType;
import com.jy.appmarket.screenlock.statistics.PasswordTypeEvent;
import com.jy.appmarket.screenlock.view.LockNumberView;
import com.rad.statistics.android.StatisticsAgent;
import com.umeng.analytics.MobclickAgent;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

/**
 * 类说明：
 * 
 * @author kalui.huang
 * @date 2015-3-4
 * @version 1.0
 */
public class SetLockerPasswordActivity extends Activity implements OnClickListener {
	private RadioButton setLockerNoPwRb;
	private RadioButton setLockerNumPwRb;
	private RadioButton setLockerPatternPwRb;
	private LinearLayout setLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_locker_password);
		initView();
	}

	private void initTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("锁屏密码");
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
	}

	private void initView() {
		initTitle();

		findViewById(R.id.set_locker_no_pw_lay).setOnClickListener(this);
		findViewById(R.id.set_locker_num_pw_lay).setOnClickListener(this);
		findViewById(R.id.set_locker_pattern_pw_lay).setOnClickListener(this);

		setLockerNoPwRb = findViewById(R.id.set_locker_no_pw_rb);
		setLockerNumPwRb = findViewById(R.id.set_locker_num_pw_rb);
		setLockerPatternPwRb = findViewById(R.id.set_locker_pattern_pw_rb);

		setLayout = findViewById(R.id.set_layout);

		setLockerNoPwRb.setOnClickListener(this);
		setLockerNumPwRb.setOnClickListener(this);
		setLockerPatternPwRb.setOnClickListener(this);

		PasswordType passwordType = LockPrefs.getPasswordType(this);
		if (passwordType == PasswordType.NO_PASSWORD) {
			setLockerNoPwRb.setChecked(true);
			setLayout.setVisibility(View.GONE);
		} else if (passwordType == PasswordType.NUM_PASSWORD) {
			setLockerNumPwRb.setChecked(true);
			setLayout.setVisibility(View.VISIBLE);
		} else if (passwordType == PasswordType.PATTERN_PASSWORD) {
			setLockerPatternPwRb.setChecked(true);
			setLayout.setVisibility(View.VISIBLE);
		}

		CheckBox touchSoundFeedbackCb = findViewById(R.id.touch_sound_feedback_cb);
		touchSoundFeedbackCb.setChecked(LockPrefs.getTouchSoundFeedback(this));
		touchSoundFeedbackCb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				LockPrefs.setTouchSoundFeedback(SetLockerPasswordActivity.this, isChecked);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			if (requestCode == 22 || requestCode == 32) {
				setNumber(2);
			} else if (requestCode == 2) {
				// 保存type=数字
				Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
				LockPrefs.setPasswordType(this, PasswordType.NUM_PASSWORD);
				sendPasswordType(PasswordType.NUM_PASSWORD);
				setLayout.setVisibility(View.VISIBLE);
			} else if (requestCode == 33 || requestCode == 23) {
				setPattern(3);
			} else if (requestCode == 3) {
				// 保存type=数字
				Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
				LockPrefs.setPasswordType(this, PasswordType.PATTERN_PASSWORD);
				sendPasswordType(PasswordType.PATTERN_PASSWORD);
				setLayout.setVisibility(View.VISIBLE);
			} else if (requestCode == 4) {
				LockPrefs.setPasswordType(this, PasswordType.NO_PASSWORD);
				sendPasswordType(PasswordType.NO_PASSWORD);
				setLayout.setVisibility(View.GONE);
			}
		} else {
			setLockerNoPwRb.setChecked(false);
			setLockerNumPwRb.setChecked(false);
			setLockerPatternPwRb.setChecked(false);
			PasswordType passwordType = LockPrefs.getPasswordType(this);
			if (passwordType == PasswordType.NO_PASSWORD) {
				setLockerNoPwRb.setChecked(true);
				setLayout.setVisibility(View.GONE);
			} else if (passwordType == PasswordType.NUM_PASSWORD) {
				setLockerNumPwRb.setChecked(true);
				setLayout.setVisibility(View.VISIBLE);
			} else if (passwordType == PasswordType.PATTERN_PASSWORD) {
				setLockerPatternPwRb.setChecked(true);
				setLayout.setVisibility(View.VISIBLE);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.set_locker_no_pw_lay:
		case R.id.set_locker_no_pw_rb:
			setLockerNoPwRb.setChecked(true);
			setLockerNumPwRb.setChecked(false);
			setLockerPatternPwRb.setChecked(false);
			setPassword(PasswordType.NO_PASSWORD);
			setLayout.setVisibility(View.GONE);
			break;
		case R.id.set_locker_num_pw_lay:
		case R.id.set_locker_num_pw_rb:
			setLockerNoPwRb.setChecked(false);
			setLockerNumPwRb.setChecked(true);
			setLockerPatternPwRb.setChecked(false);
			setPassword(PasswordType.NUM_PASSWORD);
			setLayout.setVisibility(View.VISIBLE);
			break;
		case R.id.set_locker_pattern_pw_lay:
		case R.id.set_locker_pattern_pw_rb:
			setLockerNoPwRb.setChecked(false);
			setLockerNumPwRb.setChecked(false);
			setLockerPatternPwRb.setChecked(true);
			setPassword(PasswordType.PATTERN_PASSWORD);
			setLayout.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}

	}

	private void setPassword(PasswordType type) {
		PasswordType lockType = LockPrefs.getPasswordType(this);

		// 是否设置同类型密码
		if (type == lockType) {
			if (type == PasswordType.NUM_PASSWORD) {
				changeNumber(22);
			} else if (type == PasswordType.PATTERN_PASSWORD) {
				changePattern(33);
			}
		} else {
			if (type == PasswordType.NO_PASSWORD) {
				// 设置为“无密码”
				if (lockType == PasswordType.NUM_PASSWORD) {
					changeNumber(4);
				} else if (lockType == PasswordType.PATTERN_PASSWORD) {
					changePattern(4);
				}
			} else if (type == PasswordType.NUM_PASSWORD) {
				// 修改为-->数字密码
				if (lockType == PasswordType.NO_PASSWORD) {// 无密码--->数字密码
					setNumber(2);
				} else if (lockType == PasswordType.PATTERN_PASSWORD) {// 图案密码--->数字密码
					changePattern(32);
				}
			} else {
				// 修改为--->图案密码
				if (lockType == PasswordType.NO_PASSWORD) {// 无密码--->图案密码
					setPattern(3);
				} else if (lockType == PasswordType.NUM_PASSWORD) {// 数字密码--->图案密码
					changeNumber(23);
				}
			}
		}
	}

	private void changeNumber(int requestCode) {
		Intent intent = new Intent(this, NumberLockActivity.class);
		Bundle b = new Bundle();
		b.putInt(NumberLockActivity.NUMBER_LOCK, LockNumberView.CHANGE_PASSWORD);
		intent.putExtras(b);
		startActivityForResult(intent, requestCode);
	}

	private void changePattern(int requestCode) {
		Intent intent = new Intent(this, ConfirmPatternPswActivity.class);
		startActivityForResult(intent, requestCode);
	}

	private void setNumber(int requestCode) {
		Intent intent = new Intent(this, NumberLockActivity.class);
		Bundle b = new Bundle();
		b.putInt(NumberLockActivity.NUMBER_LOCK, LockNumberView.SETTING_PASSWORD);
		intent.putExtras(b);
		startActivityForResult(intent, requestCode);
	}

	private void setPattern(int requestCode) {
		Intent intent = new Intent(this, SetPatternPasswordActivity.class);
		startActivityForResult(intent, requestCode);
	}

	/**
	 * 统计密码设置类型 0-->无密码 1-->数字密码 2-->图案密码
	 * 
	 * @param passwordType
	 */
	private void sendPasswordType(PasswordType passwordType) {
		PasswordTypeEvent event = new PasswordTypeEvent();
		event.setEventName("password_type_event");
		event.setPasswordType(passwordType.getType());
		StatisticsAgent.onEvent(this, event);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
